<!--
-->
<template>
    <vxe-modal v-model="visible" :height="layout.modalHeight(500)" :min-width="layout.modalMinWidth(600)" :position="{top: '60px'}" :title="title" :width="layout.modalWidth(600)" destroy-on-close resize show-footer show-maximize @close="cancelClick">
        <el-form ref="ELForm" v-loading="isLoading" :model="state.model" :rules="rules" :validate-on-rule-change="false" class="ELForm" inline label-width="120px">

            <el-form-item :style="layout.col1" label="名称" prop="name">
                <el-input v-model="state.model.name" />
            </el-form-item>
            <el-form-item :style="layout.col1" label="sql语句类型" prop="sqlType">
                <el-radio-group v-model="state.model.sqlType">
                    <el-radio :value="1">版本升级语句</el-radio>
                    <el-radio :value="2">手动执行语句</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item :style="layout.col1" label="sql语句" prop="sqlContent">
                <el-input type="textarea" v-model="state.model.sqlContent" :rows="5"/>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="cancelClick">取消</el-button>
            <el-button :loading="isSubmitting" type="primary" @click="beforeSubmit">
                确定
            </el-button>
        </template>
    </vxe-modal>
</template>

<script setup>
import {ref, reactive, computed, onMounted} from "vue";
import {FormMixins} from "@/utils/FormMixins";
import * as api from "./api.js";

const {ELForm, visible, title, layout, isLoading, isSubmitting, state, setOptions, submitSuccess, cancelClick} = FormMixins();
const {$message} = window;

const Model = () => {
    return {
        endVersion: null,
        sqlType: 0
    }
}

// 校验规则
const rules = computed(() => {
    return {
        programId: [{required: true, message: "请设置所属程序"}],
        name: [{required: true, message: "请设置名称"}],
        sqlType: [{required: true, message: "请设置sql语句类型"}],
        sqlContent: [{required: true, message: "请设置sql语句内容"}],
        startVersion: [{required: true, message: "请设置程序版本"}],
    }
})

// 自定义校验规则
const customRules = {
    // pid: (rule, value, callback) => {
    //     if (state.model.type > 0) {
    //         if (!value) return callback(new Error("请选择上级菜单"));
    //     }
    //     return true;
    // },
};

// 表单显示
async function show(config) {
    setOptions(config);
    state.model = Model();
    state.model = {...state.model, ...state.options}
    // 初始化数据
    api.GetProgramList({}).then(res => {
        state.programList = res.data || [];
    })
}

// 校验表单
function beforeSubmit() {
    ELForm.value.validate(isOk => {
        isOk && submit()
    })
}

// 提交表单
async function submit() {
    isSubmitting.value = true
    console.log("submit: ", state.model)
    const body = {...state.model}
    const {success, data} = await api[state.model.id ? 'UpdateProgramSQL' : 'AddProgramSQL'](body);
    isSubmitting.value = false
    if (!success) return 0;
    $message.success("操作成功")
    submitSuccess()
}

onMounted(() => {
})
defineExpose({
    show
})
</script>


<style lang="scss" scoped>
.EditForm {

}
</style>
